#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

#define HITGROUP_HEAD 1 
#define PLUGIN_VERSION "1.0"

public Plugin myinfo = 
{
	name = "L4d2_team_kill_headshot",
	author = "豆瓣酱な",
	description = "当玩家被队友爆头时直接死亡",
	version = PLUGIN_VERSION,
	url = "https://gitee.com/sweet-and-sour-bombs/adfvsdsd"
};

public void OnPluginStart()
{
	HookEvent("player_hurt", Event_PlayerHurt, EventHookMode_Post);
}

public void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	int victim = GetClientOfUserId(event.GetInt("userid"));
	int attacker = GetClientOfUserId(event.GetInt("attacker"));
	int hitgroup = event.GetInt("hitgroup");
	int damagecustom = event.GetInt("damagecustom");	

	if (!IsValidClient(victim) || !IsValidClient(attacker) || victim == attacker)
	return;
    

	if (GetClientTeam(victim) == GetClientTeam(attacker))
	{
		bool isHeadshot = (hitgroup == HITGROUP_HEAD) || (damagecustom == 1);
		if (isHeadshot && IsPlayerAlive(victim))
		{
			ForcePlayerSuicide(victim);
			PrintToChatAll("\x04[提示]\x03%s\04打爆了\x03%s\x05的狗头!", GetTrueName(attacker), GetTrueName(victim));//聊天窗提示.			
		}
	}
}

bool IsValidClient(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2 && IsPlayerAlive(client);
}

char[] GetTrueName(int client)
{
	char g_sName[32];
	int Bot = IsClientIdle(client);
	
	if(Bot != 0)
		Format(g_sName, sizeof(g_sName), "闲置:%N", Bot);
	else
		GetClientName(client, g_sName, sizeof(g_sName));
	return g_sName;
}

int IsClientIdle(int client)
{
	if (!HasEntProp(client, Prop_Send, "m_humanSpectatorUserID"))
		return 0;

	return GetClientOfUserId(GetEntProp(client, Prop_Send, "m_humanSpectatorUserID"));
}
